package com.globaldom.web.enemy;

import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.globaldom.model.CountryDetail;
import com.globaldom.model.Person;
import com.globaldom.model.PersonLocation;
import com.globaldom.utils.Constants;
import com.globaldom.utils.MemCacheManager;
import com.globaldom.utils.PMF;

@Controller
@RequestMapping(value = "/getcombatant")
public class CombatantGetController {

	public CombatantGetController() {
	}
	//
	// initializing method
	@RequestMapping(method = RequestMethod.GET)
	public String getCreateForm(Model model, HttpSession session) {
		return "territory";
	}

	@RequestMapping(value = "/get", method = RequestMethod.POST)
	public String getCombatant(@RequestParam long locID, @RequestParam long pid, Model model, HttpSession session,HttpServletRequest  request) {
		String personID = (String) session.getAttribute("personID");
		if (null == personID) {

			
			Cookie[] cookies = request.getCookies();
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				if ((cookie.getName().equals("a"))) {
					String id = cookie.getValue();
					Map<String, Object> m = MemCacheManager.getAuthStats(id);
					if (null != m)
					{
						personID = (String) m.get("personID");
						session.setAttribute("personID", personID);
					}
					else
					{return "loggedout";
					
					}
				}

			}
		
		}

	PersistenceManager pmf = PMF.get().getPersistenceManager();

		try {

			PersonLocation enemyLocation = pmf.getObjectById(PersonLocation.class, locID);
			//FIXME check if enemy == loc.getPerson
			Person enemy = pmf.getObjectById(Person.class, enemyLocation.getPerson());
			String fname = enemy.getFirstName();

			CountryDetail xcd = CountryDetail.findByCountryId(enemy.getCountryCode());
			String country = xcd.getCName();
			int level = enemy.getLevel();
			int karma = enemy.getKarma();
			model.addAttribute("fname", fname);
			model.addAttribute("nationality", country);
			model.addAttribute("level", level);
			model.addAttribute("karma", karma);
			model.addAttribute("locID", locID);
			model.addAttribute("pid", enemy.getId());
			/**
			 * Get my deployment at home!
			 * FIXME
			 */
			
//			EntityManager emx = EMF.get().createEntityManager();
//			Query query = emx.createQuery("Select sum(AvailResources),ResourceType from Deployment pl  where person=:key group by ResourceType");
//			query.setParameter("key", personID);
//			
//			PersonLocation homeLocation = (PersonLocation) query.getSingleResult();
//
//			List<Deployment> list = homeLocation.getDeployment();
//			for (Iterator<Deployment> iterator = list.iterator(); iterator.hasNext();) {
//				Deployment deployment = (Deployment) iterator.next();
//				model.addAttribute("deploy"+ deployment.getResourceType(), deployment.getAvailResources());
//			}
			

			
			model.addAttribute(Constants.CODE, "+OK");
		} catch (Exception edea) {
			// The said territory has been looted but by someone else but
			// the user is not aware!
			model.addAttribute(Constants.CODE, "-ER");
			model.addAttribute(Constants.DATA, "EDE");
		} finally {
			pmf.close();
		}
		return "enemy";
	}

}
